package com.peanut.order.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.discovery.converters.Auto;
import com.peanut.common.pojo.Car;
import com.peanut.common.pojo.Order;
import com.peanut.order.mapper.OrderMapper;
@Service
public class OrderService {
	@Autowired
	private OrderMapper peanutMapper;
	//超时提醒
	public Integer ResidualReminder(Order order) throws Exception {
		Order result=peanutMapper.selectByProduct(order);
		Date date=new Date();
		Date now = date;
		Date ordertime=result.getGetCarTime();
		Date returntime=result.getReturnTime();
		long e=returntime.getTime()-ordertime.getTime();
		long l=now.getTime()-ordertime.getTime();
		long day=l/(24*60*60*1000);
		long leaseterm=e/(24*60*60*1000);
		if(leaseterm-day<3){
			return 1;
		}else{
			return 0;
		}	
	}
	//续租
	public  Integer addTime(Integer time,Order order) {
		
		long w = time*24*60*60*1000;
		Order result= peanutMapper.selectByProduct(order);
		Date date=new Date();
		Date now = date;
		Date ordertime=result.getGetCarTime();
		Date returntime=result.getReturnTime();
		long e=returntime.getTime()-ordertime.getTime();
		long l=now.getTime()-ordertime.getTime();
		long day=l/(24*60*60*1000);
		long leaseterm=e/(24*60*60*1000);
		
		if(leaseterm-day<0){
			return 0;
		}else{
		Date q = result.getReturnTime();
		long r = q.getTime();
		long t = r+w;	
		Date y = new Date(t);
		order.setReturnTime(y);
		peanutMapper.addTime(order);
		}
		return 1;
	}
	//还车
	public Integer returncar(Order order) {
		Order result=peanutMapper.selectByProduct(order);
		Date date=new Date();
		Date now = date;
		Date ordertime=result.getGetCarTime();
		Date returntime=result.getReturnTime();
		long e=returntime.getTime()-ordertime.getTime();
		long l=now.getTime()-ordertime.getTime();
		long day=l/(24*60*60*1000);
		long leaseterm=e/(24*60*60*1000);
		if(leaseterm-day<0){
			long overtime=day-leaseterm;
			long fine=overtime*1000;
			Car car=peanutMapper.selectByCarId(order.getCarId());
			long f = Long.valueOf(car.getCarY()).longValue();
			String updateofcary= String.valueOf(f-fine);
			car.setCarY(updateofcary);
			peanutMapper.reducecardeposit(car);
			car.setCarState("0");
			peanutMapper.updatecarState(car);
			order.setReturnYn("1");
			peanutMapper.updateorderreturnYn(order);
			return 1;
		}else{
			Car car=peanutMapper.selectByCarId(order.getCarId());
			car.setCarState("0");
			peanutMapper.updatecarState(car);
			order.setReturnYn("1");
			peanutMapper.updateorderreturnYn(order);
			return 0;
		}
		
	}
	
	public void updateorder(Order order) {
	
		peanutMapper.updateOrder(order);
	}
	
	public List<Order> queryOrder(String userId) {
		
		return peanutMapper.queryOrder(userId);
		
	}
	
	public Car selectByCarId(String carId) {
		Car car= peanutMapper.selectByCarId(carId);
		return car;
	}
	public Order queryoneOrder(String orderId) {
		// TODO Auto-generated method stub
		Order order=new Order();
		order.setOrderId(orderId);
		return peanutMapper.selectByProduct(order);
	}
	// 租车
//	@Autowired
//	private RestTemplate restTemplate;
	public void zuulCar(Order order,Integer time,String money) {
		// 1. 首先将商品系统的车辆更新操作
		// "1"为出租状态  “0”为为出租状态
		order.setOrderId(UUID.randomUUID().toString());
		Car car =peanutMapper.selectByCarId(order.getCarId());
		car.setCarState("1");
		peanutMapper.updateCarState(car);
		// 1.2 执行出租状态码的更新
		car.setCarCount(car.getCarCount()+1);
		car.setCarState("1");
		peanutMapper.updateCarCount(car);
		peanutMapper.updateCarState(car);
		Date gettime=new Date();
		long w = time*24*60*60*1000;
		long s=gettime.getTime();
		long r=w+s;
		Date returntime=new Date(r);
		order.setGetCarTime(gettime);
		order.setReturnTime(returntime);
		order.setReturnYn("0");
		order.setOrderState("1");
		order.setOrderMoney(money);
		// 2. 在订单系统执行商品增加操作
		peanutMapper.insertOneOrder(order);
		
	}
}
